package com.jack.payment.entity.common;

import com.jack.payment.cache.CacheRedisServiceImpl;
import com.jack.payment.constant.enums.LoginTokenEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@Component
@Schema(name = "统一异常处理!!")
public class BusinessException extends Exception implements Serializable {
    @Schema(description = "后台返回code")
    private Integer code;
    @Schema(description = "后台返回提示")
    private String message;

    private static CacheRedisServiceImpl cacheRedisService;
    @Autowired
    public BusinessException(CacheRedisServiceImpl cacheRedisService) {
        super();
        this.cacheRedisService = cacheRedisService;
    }

    public BusinessException(LoginTokenEnum loginTokenEnum) {
            this.code = -1000;
            this.message = "重新登录一下吧";
    }

    public BusinessException(String message){
        BusinessException businessException = cacheRedisService.getExternalMessage(message);
        this.code = businessException.getCode();
        this.message = businessException.getMessage();
    }

    public BusinessException() {
        this.code = -301;
        this.message = "Server Error";
    }
}
